package com.hbnu.servlet;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

/**
 * @author kittychen
 * @since 2025-11-03 10:43
 */
public class MyServletDemo01 implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("该方法是创建Servlet实例对象时执行，主要进行Servlet实例对象的初始化的");

        String name = servletConfig.getInitParameter("name");
        String age = servletConfig.getInitParameter("age");
        System.out.println("name:" + name + " age:" + age);

        ServletContext servletContext = servletConfig.getServletContext();  // 获取ServletContext对象
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        // 给浏览器返回一个消息
        servletResponse.getWriter().write("Hello Servlet");
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("Servlet实例对象销毁时会调用该方法");
    }
}
